package com.canbot.u05.activity.base

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import com.canbot.u05.R
import com.canbot.u05.activity.BaseActivityNoTitle
import com.canbot.u05.activity.free.MediaInfo
import com.canbot.u05.helper.DialogBuildHelper
import com.canbot.u05.socket.MsgSendUtils
import com.canbot.u05.socket.MsgType
import com.canbot.u05.utils.StringUtils
import kotlinx.android.synthetic.main.base_choose_tts_activity.*

/**
 * Created by zmp on 2019/6/3 16:570
 * @author zmp
 * 文字转语音
 */
class BaseChooseTtsActivity : BaseActivityNoTitle(), View.OnClickListener {


    private var hintTip: DialogBuildHelper.HintTipBuilder? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.base_choose_tts_activity)
        initView()
    }

    private fun initView() {
        baseTttEditText.hint = StringUtils.getLinkRobotStrTwo(R.string.guide_tts_hint)
        back_btn_head.setOnClickListener(this)
        saveTextView.setOnClickListener(this)
        baseTTsIv.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.back_btn_head -> {
                finish()
            }
            R.id.saveTextView -> {
                val trim = baseTttEditText.text.toString().trim()
                if (TextUtils.isEmpty(trim)) {
                    showHintDialog(StringUtils.getLinkRobotStr(R.string.pleas_edit_text_first))
                    return
                }
                setResult(Activity.RESULT_OK, Intent()
                        .putExtra(BaseVoiceSetActivity.AUDIO_TYPE_KEY, MediaInfo.AUDIO_TYPE_TTS)
                        .putExtra(BaseVoiceSetActivity.AUDIO_PATH_OR_CONTENT, trim)
                        .putExtra(BaseVoiceSetActivity.AUDIO_SIMPLE_NAME_KEY, trim))
                finish()

            }
            R.id.baseTTsIv -> {
                val trim = baseTttEditText.text.toString().trim()
                if (TextUtils.isEmpty(trim)) {
                    showHintDialog(StringUtils.getLinkRobotStr(R.string.pleas_edit_text_first))
                    return
                }
                MsgSendUtils.sendStringMsg(MsgType.PLAY_TTS, trim)
            }
        }
    }

    private fun showHintDialog(content: String) {
        if (hintTip == null) {
            hintTip = DialogBuildHelper.createHintTipDialog(this)
                    .setClickListener(null, true)
        }
        hintTip?.setContent(content, Gravity.CENTER)?.show()
    }

}